package com.example.astronautcontact2206044090cxt1104;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ContactRVAdapter extends RecyclerView.Adapter<ContactRVAdapter.ViewHolder>{
    List<AstronautContact> contactList;
    public void setContactList(List<AstronautContact> contactList)
    {
        this.contactList = contactList;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_main_contact,parent,false);
        ViewHolder viewHolder=new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AstronautContact contact=contactList.get(position);
        holder.tvItemRVName.setText(contact.getName());
        holder.tvItemRVName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(),ContactDetailActivity.class);
                intent.putExtra("id", contact.getId());
                v.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvItemRVName;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvItemRVName = itemView.findViewById(R.id.tvItemRVName);
        }
    }
}
